package com.fingard.dsp.bank.directbank.cmbunion02;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.bankInterface.IQRPay;
import com.fingard.dsp.bank.directbank.cmbunion02.utils.SignatureUtil;
import com.fingard.dsp.bank.reqrespobj.qrpay.ReqGenQRInfo;
import com.fingard.dsp.bank.reqrespobj.qrpay.RetAtsGenQRInfo;
import com.fingard.dsp.bank.reqrespobj.unifiedorder.ReqQueryOrder;
import com.fingard.dsp.bank.reqrespobj.unifiedorder.ReqUnifiedOrder;
import com.fingard.dsp.bank.reqrespobj.unifiedorder.RetAtsQueryOrder;
import com.fingard.dsp.bank.reqrespobj.unifiedorder.RetAtsUnifiedOrder;
import com.fingard.text.StringHelper;

import java.util.HashMap;
import java.util.Map;

public class CMBUnion02QRPay extends CMB02UnifiedOrder implements IQRPay {
    @Override
    public RetAtsGenQRInfo genQRInfo(ReqGenQRInfo p_req) {
        return null;
    }

    @Override
    public RetAtsUnifiedOrder submitQROrder(ReqUnifiedOrder p_req) {
        RetAtsUnifiedOrder retAts = new RetAtsUnifiedOrder();
        retAts.rdSeq = p_req.rdSeq;
        try{
            ActSetItem actItem = this.getBankConfig().getActSetItem(p_req.ownAct, "");
            if (actItem != null) {
                if (!"NATIVE02".equalsIgnoreCase(p_req.reqHeader.commandCode)) {
                    retAts.respCode = "-1";
                    retAts.respInfo = "改线路目前只支持支付宝native码支付";
                    return retAts;
                }
                Map<String, String> requestPublicParams = getCommonMap();
                Map<String, Object> order = new HashMap<String, Object>();
                order.put("merId", actItem.merchantId);
                order.put("orderId", p_req.rdSeq);
                order.put("userId", actItem.userCode);
                order.put("notifyUrl", actItem.notifyURL);
                order.put("txnAmt", changeCurToFen(p_req.amount));
                order.put("currencyCode", "156");
                order.put("body", p_req.description);
                order.put("mchReserved", p_req.attach);
                if (StringHelper.hasAnyChar(p_req.timeExpire)) {
                    order.put("qrCodeTimeoutExpress", "20h");
                }
                if("1".equals(p_req.realNameFlag)){
                    JSONObject identity = new JSONObject();
                    identity.put("need_check","T");
                    identity.put("name", p_req.oppActName);
                    identity.put("mobile", p_req.cellPhone);
                    identity.put("type", "IDCARD");
                    identity.put("number", p_req.certNumber);
                    order.put("identity", identity.toJSONString() );
                }
                order.put("tradeScene", "INSURANCE");
                WriteBankUrlLogLn("招商聚合扫码下单地址："+bankFront.serverURL+"/zfbqrcode");
                ObjectMapper mapper = new ObjectMapper();
                requestPublicParams.put("biz_content", mapper.writeValueAsString(order));
                WriteBankLogLn("招商聚合加签前的报文内容：" + mapper.writeValueAsString(requestPublicParams));
                String signContent= SignatureUtil.getSignContent(requestPublicParams);
                String sign = SignatureUtil.rsaSign(signContent, actItem.ownKeyStorePath, "UTF-8");
                requestPublicParams.put("sign", sign);

                String[] tmpStrRet = sendToBank(sign,mapper.writeValueAsString(requestPublicParams),bankFront.serverURL+"/zfbqrcode",actItem);

                if(tmpStrRet[0].length() == 0){
                    JSONObject resultObj = JSONObject.parseObject(tmpStrRet[1]);
                    if(resultObj.get("returnCode")!=null && "SUCCESS".equals(resultObj.get("returnCode"))){
                        if(resultObj.get("respCode")!=null && "SUCCESS".equals(resultObj.get("respCode"))){
                            retAts.respCode = "0";
                            retAts.respInfo = "请求成功";
                            JSONObject bizContent = JSONObject.parseObject(resultObj.getString("biz_content"));
                            retAts.qrInfo = bizContent.getString("qrCode");
                            retAts.outTransID = bizContent.getString("cmbOrderId");
                        }else {
                            retAts.respCode = "-1";
                            retAts.respInfo = resultObj.getString("respMsg");
                        }
                    }else {
                        retAts.respCode = "-1";
                        retAts.respInfo = resultObj.getString("respMsg");
                    }
                }
            } else {
                retAts.respCode = "-1";
                retAts.respInfo = "未找到账号[" + p_req.ownAct + "]商户配置信息!";
                retAts.qrInfo = "";
            }
        }catch (Exception ex) {
            ex.printStackTrace();
            retAts.respCode = "-1";
            retAts.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }
        return retAts;
    }

    @Override
    public RetAtsQueryOrder queryQRPay(ReqQueryOrder p_req) {
        return queryUnifiedOrder(p_req);
    }
}
